package pl.edu.pjwstk.kor.database.init;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import pl.edu.pjwstk.kor.model.Address;
import pl.edu.pjwstk.kor.model.CustomDate;
import pl.edu.pjwstk.kor.model.Customer;

public class CustomerInitalizer extends PersonInitalizer<Customer> {
	private static final List<String> emailAddress = Arrays.asList("gmail.com", "onet.pl", "wp.pl", "wyborcza.pl", "gazeta.pl", "yahoo.com");
	
	private final List<Address> addresses;
	
	public CustomerInitalizer(List<Address> addresses) {
		super(Customer.class);
		this.addresses = addresses;
	}
	
	@Override
	public List<Customer> initData() throws Exception {
		List<Customer> result = new ArrayList<Customer>();
		
		for(int i = 0 ; i < 250; i++) {
			Customer customer = createRandomPerson();
			customer.setBirthDate(new CustomDate(random.nextInt(34) + 1960, random.nextInt(11) + 1, random.nextInt(27) + 1));
			
			String email = customer.getFirstName() + "." + customer.getLastName() + "@" + selectRandom(emailAddress, random);
			customer.setEmail(email.toLowerCase());
			
			customer.setLiveAddress(selectRandom(addresses, random));
			if(random.nextBoolean()) {
				customer.setMailDropAddress(customer.getLiveAddress());
			} else {
				customer.setMailDropAddress(selectRandom(addresses, random));
			}
			
			customer.setPesel(generatePesel(customer.getBirthDate()));
			result.add(customer);
		}
		
		return result;
	}
	
	private String generatePesel(CustomDate date) {
		StringBuilder sb = new StringBuilder();
		sb.append(date.getYear())
			.append(date.getMonth())
			.append(date.getDay());
		
		for(int i = 0 ; i < 5; i++) {
			sb.append(i);
		}
		return sb.toString();
	}

}
